home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / development kits / hypercard related / xcmds & xfcns / byrne's xcmds&xfcns / systemfolder.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-16  |  4.1 KB  |  175 lines

  1. /*
  2.  
  3.     SystemFolder XFCN
  4.     
  5.     © 1990 Apple Computer, Inc.; by Mike Byrne
  6.     
  7.     This XFCN returns the full pathname of the currently active System Folder.
  8.     
  9.     Form:
  10.     SystemFolder()
  11.         
  12.     
  13.     # the MPW 3.2 build commands
  14.     C -b SystemFolder.c -mbg off
  15.         Link -w -t STAK -c WILD -rt XFCN=601 ∂
  16.             -m ENTRYPOINT ∂
  17.             -sg SystemFolder ∂
  18.             SystemFolder.c.o ∂
  19.             "{Libraries}HyperXLib.o" ∂
  20.             "{Libraries}Runtime.o" ∂
  21.             "{Libraries}Interface.o" ∂
  22.             "{CLibraries}StdCLib.o" ∂
  23.             -o "some stack"
  24. */
  25.  
  26. #include <Types.h>
  27. #include <OSUtils.h>
  28. #include <Files.h>
  29. #include <string.h>
  30. #include <strings.h>
  31. #include <Memory.h>
  32. #include "HyperXCmd.h"
  33.  
  34. #define NULL 0L
  35. #define NIL  0L
  36.  
  37. #define kNumParams 0
  38.  
  39. /* prototypes */
  40. void ErrorBack(XCmdPtr paramPtr, char *message);
  41. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  42. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  43.  
  44.  
  45. pascal void EntryPoint(XCmdPtr paramPtr)
  46. {
  47.     SysEnvRec        theWorld;
  48.     CInfoPBRec        pBlck;
  49.     short            volID;
  50.     char            pathName[256];
  51.     char            cDummy[128];
  52.     Str255            pasDummy;
  53.     
  54.     
  55.     /* move high and lock the parameters. */
  56.     MoveLockParams(paramPtr, paramPtr->paramCount);
  57.  
  58.     /* check for copyright or syntax help request */
  59.     if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
  60.         ErrorBack(paramPtr, "v1.1, ©1990 Apple Computer, Inc.; by Mike Byrne");
  61.         UnlockParams(paramPtr, paramPtr->paramCount);
  62.         return;
  63.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
  64.         ErrorBack(paramPtr, "SystemFolder syntax is 'SystemFolder()'");
  65.         UnlockParams(paramPtr, paramPtr->paramCount);
  66.         return;
  67.     }
  68.  
  69.  
  70.     /* check for correct number of parameters */
  71.     if (paramPtr->paramCount != kNumParams) {
  72.         ErrorBack(paramPtr, "SystemFolder syntax is 'SystemFolder()'");
  73.         UnlockParams(paramPtr, paramPtr->paramCount);
  74.         return;
  75.     }
  76.  
  77.      /* set strings to zero */
  78.     pathName[0] = '\0';
  79.     cDummy[0] = '\0';
  80.     pasDummy[0] = '\0';
  81.  
  82.     /* get the ref number for the system folder */
  83.     if (SysEnvirons(2, &theWorld)) {
  84.         ErrorBack(paramPtr, "Can't find the System Folder");
  85.         return;
  86.     } else {
  87.         volID = theWorld.sysVRefNum;
  88.     }
  89.  
  90.     /* set up pBlck stuff */
  91.     pBlck.dirInfo.ioNamePtr = &pasDummy;
  92.     pBlck.dirInfo.ioVRefNum = volID;
  93.     pBlck.dirInfo.ioFDirIndex = 1;
  94.     pBlck.dirInfo.ioDrDirID = 0;
  95.     
  96.     /* get the first directory ID (if we can't, then errorback. */
  97.     if ( PBGetCatInfo(&pBlck, false) != noErr ) {
  98.         ErrorBack(paramPtr, "Cannot get System file info");
  99.         return;
  100.     }
  101.         
  102.     /* now, keep going up the tree things we reach the top */
  103.     while (pBlck.dirInfo.ioDrDirID != 2) {
  104.         
  105.         /* swap id's from the parent ID to the current ID and reset the index. */
  106.         pBlck.dirInfo.ioDrDirID = pBlck.dirInfo.ioDrParID;
  107.         pBlck.dirInfo.ioFDirIndex = -1;
  108.         
  109.         /* get load the NamePtr (which is equal to pasDummy) and build the C string. */
  110.         if (!PBGetCatInfo(&pBlck, false)) {
  111.             p2cstr(pasDummy);
  112.             strcpy( cDummy, pasDummy );
  113.             strcat(cDummy, ":");
  114.             strcat(cDummy, pathName);
  115.             strcpy(pathName, cDummy);
  116.  
  117.         } else {
  118.             /* this is just in case the PB call fails.  */
  119.             ErrorBack(paramPtr, "Cannot get System parent info");
  120.             return;
  121.         }
  122.     }
  123.     
  124.     
  125.     /* the pathName string should now be fully built.  we're outta here.  */
  126.     ErrorBack(paramPtr, pathName);
  127.  
  128. }
  129.  
  130.  
  131.  
  132. /*  ++++  ErrorBack ++++
  133.     allocate and load up paramPtr->returnValue with a string 
  134.    -------------------------------------------------------- */
  135. void ErrorBack(XCmdPtr paramPtr, char *message)
  136. {
  137.     Handle  mesHnd;
  138.  
  139.     /*
  140.         Allocate space for an error message.
  141.         Copy the string into it.
  142.         Return the handle to HyperCard.
  143.     */
  144.     mesHnd = NewHandle((long)(strlen(message)+1));
  145.     if (mesHnd == nil) return;
  146.     strcpy((char *)*mesHnd,message);
  147.     paramPtr->returnValue = mesHnd;
  148. }
  149.  
  150.  
  151. /*  move high and lock down all parameters  
  152.     ----------------------------------------------------------------------- */
  153. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  154. {
  155.     short i;
  156.     
  157.     for(i=0; i <= paramCount-1; i++)
  158.     {
  159.         MoveHHi(paramPtr->params[i]);
  160.         HLock(paramPtr->params[i]);
  161.     }
  162. }
  163.  
  164.  
  165.  
  166.  
  167. /* unlock all parameter handles in the XCmdBlock  
  168.    ---------------------------------------------  */
  169. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  170. {    short i;
  171.     
  172.     for(i=0; i <= paramCount-1; i++)
  173.         { HUnlock(paramPtr->params[i]);}
  174. }
  175.